
#include "stdio.h"
#include <stdint.h>
#include "../section_locate.h"
#include "pt-thread.h"
#include "my_utils.h"
#include <math.h>
#include "algorithm_collector.h"
#include "data_record.h"

static void spline_test(void);
PLACE_FUNC_AT_SEC(spline_test);


#if 0
static const uint32_t zoom[] = {1330, 1216, 1118, 1036, 966, 905, 852, 804, 762, 724, 689, 657, 628, 601, 576, 553, 531, 510, 491, 473, 456, 439, 424, 409, 395, 382, 369, 356, 344, 333, 322, 312, 301, 291, 282, 273, 264, 255, 247, 238, 230, 223, 215, 208, 201, 194, 187, 181, 174, 168, 162, 156, 150, 145, 139, 134, 129, 124, 119, 114, 110, 105, 101, 96, 92, 88, 84, 80, 76, 73, 69, 66, 62, 59, 56, 53, 50, 47, 44, 41, 38, 36, 33, 31, 29, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 7, 5, 3};
static double zoom_vs_focus[SIZEOF_ARR(zoom)] = {157, 222, 281, 332, 378, 419, 455, 489, 519, 547, 572, 596, 617, 636, 654, 671, 685, 700, 712, 723, 733, 742, 750, 757, 763, 768, 772, 775, 778, 779, 780, 781, 780, 779, 777, 774, 771, 767, 763, 758, 752, 747, 740, 732, 725, 717, 708, 699, 688, 679, 669, 657, 645, 635, 622, 610, 598, 585, 571, 557, 545, 530, 517, 499, 485, 471, 456, 440, 423, 409, 393, 379, 360, 345, 329, 312, 297, 280, 262, 244, 226, 212, 192, 177, 164, 142, 128, 112, 95, 79, 61, 42, 25, 0, 0, 0, 0, 0};



static double zoom_inv[SIZEOF_ARR(zoom)]; // 目前三次样条的横坐标是按照递增的方向计算
DEFINE_SPLINE(zoom_focus_spline, SIZEOF_ARR(zoom_inv) );
static void spline_test(void)
{
    int res = 0;

    INVERSE_ARR(zoom_vs_focus, SIZEOF_ARR(zoom_vs_focus));
    for (int i = 0; i < SIZEOF_ARR(zoom_vs_focus); i++) {
        printf("zoom_vs_focus: %f\n", zoom_vs_focus[i]);
    }

    printf("\n");


    for (int i = 0; i < SIZEOF_ARR(zoom_vs_focus); i++) {
        zoom_inv[i] = zoom[SIZEOF_ARR(zoom_vs_focus) - 1 - i];
        printf("zoom_inv: %f\n", zoom_inv[i]);
    }

    printf("SIZEOF_ARR(zoom) = %d\n", SIZEOF_ARR(zoom));
    printf("SIZEOF_ARR(zoom_vs_focus) = %d\n", SIZEOF_ARR(zoom_vs_focus));


    res = zoom_focus_spline_$set_spline_x(zoom_inv, SIZEOF_ARR(zoom_inv) );
    printf("set_spline_x res: %d\n", res);
    res = zoom_focus_spline_$set_spline_y(zoom_vs_focus, SIZEOF_ARR(zoom_vs_focus) );
    printf("set_spline_y res: %d\n", res);


//    res = zoom_focus_spline.spline(&zoom_focus_spline);   // 计算系数
    res = zoom_focus_spline_$spline();
    printf("spline res: %d\n", res);
    uint32_t zoom_pos = 0;
    while(1){
        printf("input zoom_pos:\n");
        scanf("%u", &zoom_pos);


        // 获取样条函数在该点的值
        unsigned int focus_pos = 0;
        double focus_pos_double = 0;
        focus_pos_double = zoom_focus_spline_$get_val ( (double)zoom_pos );
        printf("get_val focus_pos_double: %f\n", focus_pos_double);
        focus_pos = (unsigned int )focus_pos_double;



    }
}
#endif // 0

//static double x[] = {0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600, 3800, 4000 };
//static double y[] = {1350, 1370, 1440, 1500, 1560, 1600, 1650, 1690, 1720, 1750, 1760, 1740, 1690, 1640, 1610, 1530, 1420, 1280, 1100, 820, 610};
static double x[] = {0, 5, 10, 20, 30, 40, 50, 60, 80, 100, 125, 150, 175, 200, 250, 300, 350, 400, 450, 500, 550, 600, 700, 800, 900, 1000, 1200, 1400, 1527};
static double y[] = {627, 665, 703, 768, 827, 880, 928, 973, 1051, 1116, 1183, 1236, 1277, 1309, 1349, 1361, 1357, 1337, 1315, 1286, 1254, 1220, 1153, 1088, 1027, 968, 863, 770, 718};
DEFINE_SPLINE(zoom_focus_spline, SIZEOF_ARR(x) );

static void spline_test(void)
{
    int res;

    res = zoom_focus_spline_$set_spline_x(x, SIZEOF_ARR(x) );
    printf("set_spline_x res: %d\n", res);
    res = zoom_focus_spline_$set_spline_y(y, SIZEOF_ARR(y) );
    printf("set_spline_y res: %d\n", res);

    res = zoom_focus_spline_$spline();
    printf("spline res: %d\n", res);

    int x_cnt = x[sizeof(x) / sizeof(x[0]) - 1];
    printf("spline x_cnt: %d\n", x_cnt);

    for(int i = 0; i <= x_cnt; i++){
        double poly_val = zoom_focus_spline_$get_val ( (double)i );
//        printf("get_val poly_val: %d\n", (int)poly_val);
        printf("%d, ", (int)poly_val);
        if( (i % 20) == 0){
            printf("\n");
        }

    }

}

